package org.jeecg.modules.demo.gsy.service.impl;

import org.jeecg.modules.demo.gsy.entity.Canteen;
import org.jeecg.modules.demo.gsy.entity.Dishes;
import org.jeecg.modules.demo.gsy.mapper.DishesMapper;
import org.jeecg.modules.demo.gsy.mapper.CanteenMapper;
import org.jeecg.modules.demo.gsy.service.ICanteenService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 食堂
 * @Author: jeecg-boot
 * @Date:   2021-10-28
 * @Version: V1.0
 */
@Service
public class CanteenServiceImpl extends ServiceImpl<CanteenMapper, Canteen> implements ICanteenService {

	@Autowired
	private CanteenMapper canteenMapper;
	@Autowired
	private DishesMapper dishesMapper;
	
	@Override
	@Transactional
	public void saveMain(Canteen canteen, List<Dishes> dishesList) {
		canteenMapper.insert(canteen);
		if(dishesList!=null && dishesList.size()>0) {
			for(Dishes entity:dishesList) {
				//外键设置
				entity.setCanteenId(canteen.getId());
				dishesMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(Canteen canteen,List<Dishes> dishesList) {
		canteenMapper.updateById(canteen);
		
		//1.先删除子表数据
		dishesMapper.deleteByMainId(canteen.getId());
		
		//2.子表数据重新插入
		if(dishesList!=null && dishesList.size()>0) {
			for(Dishes entity:dishesList) {
				//外键设置
				entity.setCanteenId(canteen.getId());
				dishesMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		dishesMapper.deleteByMainId(id);
		canteenMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			dishesMapper.deleteByMainId(id.toString());
			canteenMapper.deleteById(id);
		}
	}
	
}
